local waterrun = {}

function waterrun.onInitAPI()
	registerEvent(waterrun, "onTick")
	registerEvent(waterrun, "onDraw")
end

local function DeleteBlock(plyr)
    if gplayerData[plyr.idx].WaterHoverBlock and gplayerData[plyr.idx].WaterHoverBlock.isValid then
        gplayerData[plyr.idx].WaterHoverBlock:delete()
    end
end

local countdownstart = 500

-- [[ Replenish meter, if not directly above water]] --
local function ReplenishBoots(plyr)
    if gplayerData[plyr.idx].WaterCounter ~= countdownstart then
        if not plyr:mem(0x36,FIELD_BOOL) then
            local AddValue = false
            local checkx1 = plyr.x + (plyr.width/2)
            local checky1 = plyr.y + plyr.height
            local checkx2 = plyr.x + (plyr.width/2)
            local checky2 = plyr.sectionObj.boundary.bottom -- this makes it so it scans from the player's bottom to the bottom of their current section
            --Graphics.drawLine{x1 = checkx1 - camera.x, y1 = checky1 - camera.y, x2 = checkx2- camera.x, y2 = checky2 - camera.y}
            local waterbelow = Liquid.getIntersecting(checkx1, checky1, checkx2, checky2)
            local blockcheck = Block.getIntersecting(checkx1, checky1, checkx2, checky2)
            if next(waterbelow) ~= nil then
                if next(blockcheck) ~= nil then
                    for blodex, blck in pairs(blockcheck) do
                        if blck.id ~= 869 and blck.id ~= 0 then
                            --Text.print(math.abs(blck.y) .. " " .. blck.id, 20, 240+(20*blodex))
                            for liqdex, liquid in ipairs(waterbelow) do
                                --Text.print(math.abs(liquid.y), 250, 240+(20*liqdex))
                                if math.abs(blck.y) > math.abs(liquid.y) then
                                    AddValue = true
                                    --Text.print("block found above water", 20, 400)
                                end
                            end
                        end
                    end
                else
                    AddValue = false
                end
            else
                AddValue = true
                --Text.print("no water detected", 20, 400)
            end
            if AddValue then
                gplayerData[plyr.idx].WaterCounter = math.min(countdownstart, gplayerData[plyr.idx].WaterCounter + 0.65)
            end
        end
    end
end

function waterrun.onTick()
    if not globalsave.WaterBoots then return end
    if globalsave.HardMode then
        countdownstart = 450
    else
        countdownstart = 500
    end
    for _, plyr in ipairs(Player.get()) do
        if not globalsave["WaterShoesOn" .. plyr.idx] then
            DeleteBlock(plyr)
            if gplayerData[plyr.idx].WaterCounter then
                ReplenishBoots(plyr)
            end
            break
        end
        if not gplayerData[plyr.idx].WaterCounter then
            gplayerData[plyr.idx].WaterCounter = countdownstart
            gplayerData[plyr.idx].WaterHUDO = 0
        end
        local camerathing = camera
        if plyr == player2 and (camerathing.width ~= 800) then
            camerathing = camera2
        end
        local checkx1 = plyr.x + (plyr.width/2)
        local checky1 = plyr.y + plyr.height
        local checkx2 = plyr.x + (plyr.width/2)
        local checky2 = plyr.y + plyr.height + 16
        -- all this in place should mean it will check 16 units below the player

        if not plyr.keys.down then
            -- [[ Spawn Platform ]] --
            local footwater = Liquid.getIntersecting(checkx1, checky1, checkx2, checky2)
            if next(footwater) ~= nil then
                if gplayerData[plyr.idx].WaterCounter > 0 then
                    for _, liquid in ipairs(footwater) do
                        --Text.print(liquid.y, 20, 260)
                        DeleteBlock(plyr)
                        if plyr.y+plyr.height < liquid.y and not liquid.isQuicksand then
                            --Text.print(liquid.y-4, 20, 100)
                            gplayerData[plyr.idx].WaterHoverBlock = Block.spawn(869, plyr.x, liquid.y-4)
                            if plyr.forcedState == 0 then
                                gplayerData[plyr.idx].WaterCounter = math.max(0, gplayerData[plyr.idx].WaterCounter - 1.05)
                            end
                            --Text.print("block time", 20, 280)
                        end
                    end
                else
                    DeleteBlock(plyr)
                end
            else
                ReplenishBoots(plyr)
                DeleteBlock(plyr)
            end
        else
            DeleteBlock(plyr)
        end
    end
end


function waterrun.onDraw()
    if not globalsave.WaterBoots then return end
    local Meter = Graphics.loadImage(Misc.resolveFile("GFX/boot-meter" .. globalsave.WBHUD .. ".png"))
    local MeterE = Graphics.loadImage(Misc.resolveFile("GFX/boot-meter" .. globalsave.WBHUD .. "-e.png"))
    for _, plyr in ipairs(Player.get()) do
        if plyr.deathTimer ~= 0 or not gplayerData[plyr.idx].WaterCounter then
            break
        end
        --Text.print(math.ceil(gplayerData[plyr.idx].WaterCounter/10), 20, 200)
        if gplayerData[plyr.idx].WaterCounter ~= countdownstart then
            gplayerData[plyr.idx].WaterHUDO = math.min(1, gplayerData[plyr.idx].WaterHUDO + 0.08)
        else
            gplayerData[plyr.idx].WaterHUDO = math.max(0, gplayerData[plyr.idx].WaterHUDO - 0.14)
        end
        local MeterLength = math.ceil((MeterE.width * gplayerData[plyr.idx].WaterCounter) / countdownstart)
        Graphics.drawImageToScene(MeterE, player.x + ((player.width/2) - (Meter.width/2)), player.y - 32 - 8, gplayerData[plyr.idx].WaterHUDO)
            Graphics.drawImageToScene(Meter, player.x + ((player.width/2) - (Meter.width/2)), player.y - 32 - 8, 0, 0, MeterLength, Meter.height, gplayerData[plyr.idx].WaterHUDO)
        --
    end
end

return waterrun